package com.zxs.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import parm.UserCheckParm;
import parm.UserLoginParm;
import pojo.User;
import com.zxs.user.service.UserService;
import utils.R;

/**
 * @author zxs
 * *7 9:14
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;

    @PostMapping("check")
    public R check(@RequestBody @Validated UserCheckParm parameter, BindingResult result){
        if (result.hasErrors()){
            return R.fail("账号为空，请重新输入");
        }
        //账号不为空，则查询数库是否账号是否存在
       return userService.check(parameter);
    }

    @PostMapping("register")
    public R register( @RequestBody @Validated User user, BindingResult result){
        //1.如果某个参数为空，返回错误信息
        if (result.hasErrors())
            return R.fail("某个参数为空，重新输入！");
        //否则调用业务方法进行逻辑判断
        return userService.register(user);
    }
    @PostMapping("login")
    public R login(@RequestBody @Validated UserLoginParm userLoginParm,BindingResult result){
        if(result.hasErrors()){
            return  R.fail("参数异常，请重新输入");
        }
        return userService.login(userLoginParm);
    }
}
